import { MockMethod } from 'vite-plugin-mock'
import { ApiResponse } from '../../src/model/common-model'
import { faker } from '@faker-js/faker'

// 模拟项目目录结构
const projectPackages: Record<string, Array<{ filename: string; version: string; path: string }>> = {
  'sdkverify-admin': [
    { filename: 'sdkverify-admin-1.0.0.zip', version: '1.0.0', path: '/Users/wusheng/Desktop/sdkverify-admin/sdkverify-admin-1.0.0.zip' },
    { filename: 'sdkverify-admin-1.0.1.zip', version: '1.0.1', path: '/Users/wusheng/Desktop/sdkverify-admin/sdkverify-admin-1.0.1.zip' },
    { filename: 'sdkverify-admin-1.1.0.zip', version: '1.1.0', path: '/Users/wusheng/Desktop/sdkverify-admin/sdkverify-admin-1.1.0.zip' }
  ],
  'push-admin': [
    { filename: 'push-admin-2.0.0.zip', version: '2.0.0', path: '/Users/wusheng/Desktop/push-admin/push-admin-2.0.0.zip' },
    { filename: 'push-admin-2.1.0.zip', version: '2.1.0', path: '/Users/wusheng/Desktop/push-admin/push-admin-2.1.0.zip' }
  ],
  'analytics-portal': [
    { filename: 'analytics-portal-3.0.0.zip', version: '3.0.0', path: '/Users/wusheng/Desktop/analytics-portal/analytics-portal-3.0.0.zip' }
  ]
}

export default [
  // 获取项目包版本列表
  {
    url: '/api/v1/packages',
    method: 'get',
    response: (req: any) => {
      const { project_name } = req.query

      if (!project_name) {
        return {
          code: 1,
          errorMsg: '项目名称不能为空',
          data: null
        }
      }

      // 根据项目名获取包列表
      const packages = projectPackages[project_name] || []

      // 按版本号降序排序
      packages.sort((a, b) => {
        const aParts = a.version.split('.').map(Number)
        const bParts = b.version.split('.').map(Number)
        
        for (let i = 0; i < 3; i++) {
          if (aParts[i] !== bParts[i]) {
            return bParts[i] - aParts[i]
          }
        }
        return 0
      })

      return {
        code: 0,
        errorMsg: '',
        data: packages
      }
    }
  }
] as MockMethod[]
